using System;
using System.Xml;
using Symbonix.NorSoNet.Security.DAL.Repositories;
using Symbonix.NorSoNet.Security.DAL.Entities;
using System.Collections.Generic;
namespace Symbonix.NorSoNet.Security.BLL.Membership {
	
	public class SocialNetworkUserProfile : UserProfileBase  {

        public override int Id
        {
            get;
            protected set;
        }

        public override String ImageUrl
        {
            get;
            set;
        }
		
		public override int ForUserId {
            get;
            set;
		}
		
		public override DateTime DateOfBirth {
            get;
            set;
		}
		
		public override String AboutMe {
            get;
            set;
		}
		
		public override String City {
            get;
            set;
		}
		
		public override String Area {
            get;
            set;
		}
		
		public override String Country {
            get;
            set;
		}
		
		public override String ZipCode {
            get;
            set;
		}
		
		public override Options.Gender GenderOption {
            get;
            set;
		}
		
		public override String GeneralInterest {
            get;
            set;
		}
		
		public override String MovieInterest {
            get;
            set;
		}
		
		public override String MusicInterest {
            get;
            set;
		}
		
		public override String TVInterest {
            get;
            set;
		}
		
		public override String BooksInterest {
            get;
            set;
		}
		
		public override String GameInterest {
            get;
            set;
		}
		
		public override String SportInterest {
            get;
            set;
		}
		
        public override List<Occupation> Occupations
        {
            get;
            set;
		}

        public override List<Education> Educations
        {
            get;
            set;
		}

        protected SocialNetworkUserProfile() { }

        protected internal static IUserProfile GetInstance() { return new SocialNetworkUserProfile(); }

        /// <summary>
        /// Creates a new empty social network user profile instance for a IUser
        /// [Ronald]
        /// </summary>
        /// <param name="user">IUser</param>
        /// <returns>new empty social network user profile instance</returns>
        public override IUserProfile Create(IUser user)
        {
            ProfileData profileData = new ProfileData();
            profileData.UserID = user.Id;
            profileData.DateOfBirth = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
            profileData.Gender = Options.Gender.NotSpecified.ToString();

            ProfileDataRepository profileDataRepository = new ProfileDataRepository();
            profileData = profileDataRepository.Insert(profileData);
            if (profileData != null)
                return (IUserProfile)LoadFromProfileData(profileData, null);
            return null;

        }

        /// <summary>
        /// Load social network user profile instance for a user by user id
        /// [Ronald]
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <returns>social network user profile instance</returns>
        public override IUserProfile Load(int userId)
        {
            ProfileDataRepository profileDataRepository = new ProfileDataRepository();
            ProfileData profileData = profileDataRepository.GetProfileDataByUserID(userId);
            if (profileData != null)
                return (IUserProfile)LoadFromProfileData(profileData, null);
            return null;
		}

        /// <summary>
        /// Update social network user profile
        /// [Ronald]
        /// </summary>
        /// <returns>social network user profile instance</returns>
        public override IUserProfile Update()
        {
            ProfileDataRepository profileDataRepository = new ProfileDataRepository();
            ProfileData profileData = profileDataRepository.GetProfileDataByUserID(this.ForUserId);
            LoadFromSocialNetworkUserProfile(this, profileData);
            profileDataRepository.Update(profileData);
            return this;
        }

        /// <summary>
        /// Creates a social network user profile instance from profile data
        /// [Ronald]
        /// </summary>
        /// <param name="profileData">Profile data</param>
        /// <returns>social network user profile instance</returns>
        protected SocialNetworkUserProfile LoadFromProfileData(ProfileData profileData, SocialNetworkUserProfile socialNetworkUserProfile)
        {
            if (socialNetworkUserProfile == null)
                socialNetworkUserProfile = new SocialNetworkUserProfile();
            socialNetworkUserProfile.Id = profileData.ProfileID;
            socialNetworkUserProfile.ForUserId = profileData.UserID;

            socialNetworkUserProfile.AboutMe = profileData.AboutMe;
            socialNetworkUserProfile.Area = profileData.Address;
            socialNetworkUserProfile.BooksInterest = profileData.BooksInterest;
            socialNetworkUserProfile.City = profileData.City;
            socialNetworkUserProfile.Country = profileData.Country;
            socialNetworkUserProfile.DateOfBirth = profileData.DateOfBirth;
            socialNetworkUserProfile.Educations = Education.DeSerialize(profileData.Education);
            socialNetworkUserProfile.GameInterest = profileData.GameInterest;
            socialNetworkUserProfile.GenderOption = (Options.Gender)Enum.Parse(typeof(Options.Gender), profileData.Gender);
            socialNetworkUserProfile.GeneralInterest = profileData.GeneralInterest;
            socialNetworkUserProfile.ImageUrl = profileData.ImageUrl;
            socialNetworkUserProfile.MovieInterest = profileData.MovieInterest;
            socialNetworkUserProfile.MusicInterest = profileData.MusicInterest;
            socialNetworkUserProfile.Occupations = Occupation.DeSerialize(profileData.Occupation);
            socialNetworkUserProfile.SportInterest = profileData.SportInterest;
            socialNetworkUserProfile.TVInterest = profileData.TVInterest;
            socialNetworkUserProfile.ZipCode = profileData.ZipCode;

            return socialNetworkUserProfile;
        }

        /// <summary>
        /// Creates a social network user profile instance from profile data
        /// [Ronald]
        /// </summary>
        /// <param name="profileData">Profile data</param>
        /// <returns>social network user profile instance</returns>
        protected ProfileData LoadFromSocialNetworkUserProfile(SocialNetworkUserProfile socialNetworkUserProfile, ProfileData profileData)
        {
            if (profileData==null)
                profileData = new ProfileData();
            profileData.ProfileID = socialNetworkUserProfile.Id;
            profileData.UserID = socialNetworkUserProfile.ForUserId;

            profileData.AboutMe = socialNetworkUserProfile.AboutMe;
            profileData.Address = socialNetworkUserProfile.Area;
            profileData.BooksInterest = socialNetworkUserProfile.BooksInterest;
            profileData.City = socialNetworkUserProfile.City;
            profileData.Country = socialNetworkUserProfile.Country;
            profileData.DateOfBirth = socialNetworkUserProfile.DateOfBirth;
            profileData.Education = Education.Serialize(socialNetworkUserProfile.Educations);
            profileData.GameInterest = socialNetworkUserProfile.GameInterest;
            profileData.Gender = socialNetworkUserProfile.GenderOption.ToString();
            profileData.GeneralInterest = socialNetworkUserProfile.GeneralInterest;
            profileData.ImageUrl = socialNetworkUserProfile.ImageUrl;
            //profileData.MaritalStatus
            profileData.MovieInterest = socialNetworkUserProfile.MovieInterest;
            profileData.MusicInterest = socialNetworkUserProfile.MusicInterest;
            profileData.Occupation = Occupation.Serialize(socialNetworkUserProfile.Occupations);
            //profileData.Religion
            profileData.SportInterest = socialNetworkUserProfile.SportInterest;
            //profileData.State
            profileData.TVInterest = socialNetworkUserProfile.TVInterest;
            profileData.ZipCode = socialNetworkUserProfile.ZipCode;

            return profileData;
        }
    }

}
